(function ($) {
    'use strict';

    /**
      * @description Globally defined function allowing for cross-browser file uploads
      * 
      * @params {object} params All params detailed below
      * @params {string} url Url to post to. Defaults to '/propURL'
      * @params {array} data An array of objects with name/value pairs for all other fields
      * @params {array} fileFieldIds An array of strings that correlate to file input Ids.
      * 
      * @params {function} successCallback A function you can pass in to handle the successs response from the server (can be the same as _call)
      * @params {function} failureCallback A function you can pass in to handle the error response from the server (can be the same as _call)
      * 
      * @returns {function} Attaches 'superUploader' method to the window for use globally.
    */
    var _superUploader = function (params, successCallback, failureCallback) {

        if (!params) {
            params = {};
        }
        // add security token
        var key = IParameterName.AJAX_TOKEN;
        var value = user.ajaxSecurityToken;
        params.data[key] = value;
        
        function stringTheIds() {
            var x = params.fileFieldIds || [],
                i;

            for (i = 0; i < x.length; i++) {
                x[i] = " #" + x[i];
            }

            return x.toString();

        }

        $.ajax({
            url: params.url || '/scholaroneAJAXServlet',
            method: 'POST',
            contentType : "multipart/form-data",
            data: params.data || [],
            dataType: 'json',
            files: $(stringTheIds(params.fileFieldIds)),
            iframe: true,
            processData: false
        }).done(function (jsonObj) {
        	// with the iframe submit, we need to clear this on return
            // see scholarone.js newSubmit.js
            submissionInProgress = 0;
        	S1MUtils.ajaxDone(jsonObj,successCallback,failureCallback);
            
        }).fail(function (err) {
        	// with the iframe submit, we need to clear this on return
            // see scholarone.js newSubmit.js
            submissionInProgress = 0;
        	S1MUtils.ajaxFail(err);
            
        });
    };

    // Export it into global window for use.
    window.superUploader = _superUploader;

}(jQuery));
